package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.platform.dto.BasicInstallLocationDTO;
import com.iwomy.secureplat.platform.entity.BasicInstallLocationEntity;
import com.iwomy.secureplat.platform.service.BasicInstallLocationService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 安装点位表
 *
 * @author pig
 * @date 2023-11-21 15:12:39
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicInstallLocation")
@Tag(description = "basicInstallLocation", name = "安装点位表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicInstallLocationController {

	private final BasicInstallLocationService basicInstallLocationService;

	/**
	 * 分页查询
	 *
	 * @param page                    分页对象
	 * @param basicInstallLocationDTO 安装点位表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_view')")
	public R getBasicInstallLocationPage(@ParameterObject Page page, @ParameterObject BasicInstallLocationDTO basicInstallLocationDTO) {
		return R.ok(basicInstallLocationService.getBasicInstallLocationPage(page, basicInstallLocationDTO));
	}


	/**
	 * 通过id查询安装点位表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicInstallLocationService.getByIds(id));
	}

	/**
	 * 新增安装点位表
	 *
	 * @param basicInstallLocation 安装点位表
	 * @return R
	 */
	@Operation(summary = "新增安装点位表", description = "新增安装点位表")
	@SysLog("新增安装点位表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_add')")
	public R save(@RequestBody BasicInstallLocationEntity basicInstallLocation) {
		return basicInstallLocationService.newlyAdded(basicInstallLocation);

	}

	/**
	 * 修改安装点位表
	 *
	 * @param basicInstallLocation 安装点位表
	 * @return R
	 */
	@Operation(summary = "修改安装点位表", description = "修改安装点位表")
	@SysLog("修改安装点位表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_edit')")
	public R updateById(@RequestBody BasicInstallLocationEntity basicInstallLocation) {
		return R.ok(basicInstallLocationService.updateById(basicInstallLocation));
	}

	/**
	 * 通过id删除安装点位表
	 *
	 * @param basicInstallLocationDTO
	 * @return R
	 */
	@Operation(summary = "通过id删除安装点位表", description = "通过id删除安装点位表")
	@SysLog("通过id删除安装点位表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_del')")
	public R removeById(@RequestBody BasicInstallLocationDTO basicInstallLocationDTO) {
		return basicInstallLocationService.newlyRemoveById(basicInstallLocationDTO);
	}

	/**
	 * 分页查询
	 *
	 * @param basicInstallLocationDTO 安装点位
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_view')")
	public R getBasicInstallLocationList(@ParameterObject BasicInstallLocationDTO basicInstallLocationDTO) {
		LambdaQueryWrapper<BasicInstallLocationEntity> wrapper = Wrappers.lambdaQuery();
		if (StrUtil.isNotEmpty(basicInstallLocationDTO.getAddress())) {
			wrapper.like(BasicInstallLocationEntity::getAddress, basicInstallLocationDTO.getAddress());
		}
		return R.ok(basicInstallLocationService.list(wrapper));
	}

	/**
	 * 导出excel 表格
	 *
	 * @param basicInstallLocation 查询条件
	 * @param ids                  导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_export')")
	public List<BasicInstallLocationEntity> export(BasicInstallLocationEntity basicInstallLocation, Long[] ids) {
		return basicInstallLocationService.list(Wrappers.lambdaQuery(basicInstallLocation).in(ArrayUtil.isNotEmpty(ids), BasicInstallLocationEntity::getId, ids));
	}

	@Operation(summary = "大屏设备显示信息", description = "大屏设备显示信息")
	@GetMapping("/screen")
	@PreAuthorize("@pms.hasPermission('platform_basicInstallLocation_view')")
	public R getScreen() {
		return R.ok(basicInstallLocationService.getScreen());
	}

	/**
	 * 安装点变更imei号
	 *
	 * @param basicInstallLocation 安装点变更imei号
	 * @return R
	 */
	@Operation(summary = "修改安装点变更imei号", description = "修改安装点变更imei号")
	@SysLog("修改安装点变更imei号")
	@PutMapping("/replaceImei")
	public R replaceImei(@RequestBody BasicInstallLocationDTO basicInstallLocation) {
		return basicInstallLocationService.replaceImei(basicInstallLocation);
	}

	/**
	 * 批量新增安装点位表
	 *
	 * @param basicInstallLocation 安装点位表
	 * @return R
	 */
	@Operation(summary = "批量新增安装点位表", description = "批量新增安装点位表")
	@SysLog("新增安装点位表")
	@PostMapping("/saveBatch")
	public R saveBatch(@RequestBody BasicInstallLocationDTO basicInstallLocation) {
		return basicInstallLocationService.newlySaveBatch(basicInstallLocation);

	}

}